local skel = fk.CreateSkill {
  name = "efengqi__ganglie",
}

Fk:loadTranslationTable{
  ["efengqi__ganglie"] = "刚烈",
  [":efengqi__ganglie"] = "一名角色的结束阶段，你可以弃置任意名本回合受到过伤害的角色各一张手牌，若含你，你对一名角色造成1点伤害，否则，你进行【浮雷】判定。",

  ["#efengqi__ganglie-choose"] = "刚烈：你可以弃置任意名本回合受到过伤害的角色各一张手牌",
  ["#efengqi__ganglie-damage"] = "刚烈：请选择1名角色，你对其造成1点伤害",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target.phase == Player.Finish and player:hasSkill(skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = {}
    room.logic:getActualDamageEvents(1, function(e) table.insertIfNeed(targets, e.data.to) end)
    targets = table.filter(targets, function(p) return not p.dead and not p:isKongcheng() end)
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, { targets = targets, min_num= 1, max_num = #targets,
    prompt= "#efengqi__ganglie-choose", skill_name= skel.name })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos ---@type ServerPlayer[]
    for _, to in ipairs(tos) do
      if not to:isKongcheng() then
        local cid = room:askToChooseCard(player, { target = to, flag = "h", skill_name = skel.name})
        room:throwCard(cid, skel.name, to, player)
      end
      if player.dead then return end
    end
    if table.contains(tos, player) then
      tos = room:askToChoosePlayers(player, {
        targets = room.alive_players, min_num = 1, max_num = 1,
        prompt = "#efengqi__ganglie-damage", skill_name = skel.name, cancelable = false,
      })
      if #tos > 0 then
        room:damage { from = player, to = tos[1], damage = 1, skillName = skel.name }
      end
    else
      local judge = {
        who = player,
        reason = "floating_thunder",
        pattern = ".|.|spade",
      }
      room:judge(judge)
      if judge.card.suit == Card.Spade and not player.dead then
        room:damage{
          to = player,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = skel.name,
        }
      end
    end
  end,
})

return skel
